package com.redwood.report.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.redwood.report.R;
import com.redwood.report.activity.ReportActivity;
import com.redwood.report.activity.TongjiActivity;
import com.redwood.report.entity.BarDataBeen;
import com.redwood.report.entity.TongJiBaoBiaoBeen;
import com.redwood.report.entity.TongjiInfo;
import com.redwood.report.entity.TongjiPieChartBeen;
import com.redwood.report.entity.TongjiRongChartBeen;
import com.redwood.report.entity.TongjibarChartBeen;
import com.redwood.report.fragment.BaoXiuFragment;
import com.redwood.report.widget.RingCustomView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import static com.redwood.report.activity.TongjiActivity.dayType;
import static com.redwood.report.activity.TongjiActivity.type;

/**
 * 统计item
 *
 * Created by Supeng on 2018/8/23.
 */
//Caption been
public class TongJiItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity,BaseViewHolder> {
    public static final int TYPE_LEVEL_1 = 0;
    public static final int TYPE_LEVEL_2 = 1;
    public static final int TYPE_LEVEL_3 = 2;
    public static final int TYPE_LEVEL_4 = 3;
    public BarChart chart_barchart_jitai;

    public void setClickLinster(ClickLinster clickLinster) {
        this.clickLinster = clickLinster;
    }

    private ClickLinster clickLinster;

    public MenuCheckedChangeListener menuCheckedChangeListener=new MenuCheckedChangeListener();
    public TongJiItemAdapter(List data) {
        super(data);
        addItemType(TYPE_LEVEL_1, R.layout.layout_tongji_item);
        addItemType(TYPE_LEVEL_2, R.layout.layout_tongji_pie_item);
        addItemType(TYPE_LEVEL_3, R.layout.layout_tongjibar_item);
        addItemType(TYPE_LEVEL_4, R.layout.layout_tongjirond_item);

    }

    @Override
    protected void convert(final BaseViewHolder holder, MultiItemEntity itemTemp) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_1:
                TongJiBaoBiaoBeen baoBiaoBeen= (TongJiBaoBiaoBeen) itemTemp;
                int xvhao=(holder.getAdapterPosition());
                if(baoBiaoBeen.getIsTitle()){
                    holder.setText(R.id.tv_jitai,mContext.getResources().getString(R.string.jitai));
                    holder.setText(R.id.tv_yunxing,mContext.getResources().getString(R.string.yunxing));
                    holder.setText(R.id.tv_guzhang,mContext.getResources().getString(R.string.guzhang));
                    holder.setText(R.id.tv_daiji,mContext.getResources().getString(R.string.daiji));
                    holder.setText(R.id.tv_lixian,mContext.getResources().getString(R.string.lixian));
                    holder.setText(R.id.tv_chanliang,mContext.getResources().getString(R.string.chanliang2));
                }else{
                    holder.setText(R.id.tv_jitai,baoBiaoBeen.getJitai());
                    holder.setText(R.id.tv_yunxing, com.redwood.report.util.Utils.getStingToHour2(baoBiaoBeen.getYunxing()));
                    holder.setText(R.id.tv_guzhang,com.redwood.report.util.Utils.getStingToHour2(baoBiaoBeen.getGuzhang()));
                    holder.setText(R.id.tv_daiji,com.redwood.report.util.Utils.getStingToHour2(baoBiaoBeen.getDaiji()));
                    holder.setText(R.id.tv_lixian,com.redwood.report.util.Utils.getStingToHour2(baoBiaoBeen.getLixian()));
                    holder.setText(R.id.tv_chanliang,com.redwood.report.util.Utils.stringchanliang(baoBiaoBeen.getChanliang()));
                }
                if(xvhao%2!=0){
                    holder.setBackgroundColor(R.id.ll_item_background,mContext.getResources().getColor(R.color.blueColorAppKey3));
                }else{
                    holder.setBackgroundColor(R.id.ll_item_background,mContext.getResources().getColor(R.color.blueColorAppKey));
                }
               break;
            case TYPE_LEVEL_2:
                TongjiPieChartBeen tongjiPieChartBeen= (TongjiPieChartBeen) itemTemp;
               PieChart chart_jitai=holder.getView(R.id.chart_jitai);
                initChartOne(chart_jitai);
                setTSData(chart_jitai,tongjiPieChartBeen.getZaixian(),tongjiPieChartBeen.getLixian(),tongjiPieChartBeen.getJingdao(),tongjiPieChartBeen.getDaiji());
                break;
            case TYPE_LEVEL_3:
                TongjibarChartBeen tongjibarChartBeen=(TongjibarChartBeen) itemTemp;
                List<TongjiInfo.InfoBean.TubiaoBean> tubiaoBeanList=tongjibarChartBeen.getTubiaoBeanList();
                chart_barchart_jitai=holder.getView(R.id.chart_bar);
                initChartSet(chart_barchart_jitai);
                List<String> xvalueList=new ArrayList<>();
                List<Integer> yvalueList=new ArrayList<>();
                boolean hasData=false;
                if(tubiaoBeanList!=null&&tubiaoBeanList.size()>0) {
                    for (TongjiInfo.InfoBean.TubiaoBean tubiaoBean : tubiaoBeanList) {
                        if ("week".equals(dayType)) {
                            switch (tubiaoBean.getRiqi()) {
                                case "1":
                                    xvalueList.add("星期一");
                                    break;
                                case "2":
                                    xvalueList.add("星期二");
                                    break;
                                case "3":
                                    xvalueList.add("星期三");
                                    break;
                                case "4":
                                    xvalueList.add("星期四");
                                    break;
                                case "5":
                                    xvalueList.add("星期五");
                                    break;
                                case "6":
                                    xvalueList.add("星期六");
                                    break;
                                case "7":
                                    xvalueList.add("星期日");
                                    break;

                            }

                            yvalueList.add((int) (tubiaoBean.getMiaoshu() / 3600));
                        } else if ("month".equals(dayType)) {
                            switch (tubiaoBean.getRiqi()) {
                                case "01":
                                    xvalueList.add("1月");
                                    break;
                                case "02":
                                    xvalueList.add("2月");
                                    break;
                                case "03":
                                    xvalueList.add("3月");
                                    break;
                                case "04":
                                    xvalueList.add("4月");
                                    break;
                                case "05":
                                    xvalueList.add("5月");
                                    break;
                                case "06":
                                    xvalueList.add("6月");
                                    break;
                                case "07":
                                    xvalueList.add("7月");
                                    break;
                                case "08":
                                    xvalueList.add("8月");
                                    break;
                                case "09":
                                    xvalueList.add("9月");
                                    break;
                                case "10":
                                    xvalueList.add("10月");
                                    break;
                                case "11":
                                    xvalueList.add("11月");
                                    break;
                                case "12":
                                    xvalueList.add("12月");
                                    break;

                            }

                            yvalueList.add((int) (tubiaoBean.getMiaoshu() / 3600));
                        } else if ("jidu".equals(dayType)) {
                            switch (tubiaoBean.getRiqi()) {
                                case "1":
                                    xvalueList.add("一季度");
                                    break;
                                case "2":
                                    xvalueList.add("二季度");
                                    break;
                                case "3":
                                    xvalueList.add("三季度");
                                    break;
                                case "4":
                                    xvalueList.add("四季度");
                                    break;

                            }

                            yvalueList.add((int) (tubiaoBean.getMiaoshu() / 3600));
                        }
                        if(tubiaoBean.getMiaoshu()!=0){
                            hasData=true;
                        }
                    }
                }
                if(hasData) {
                    initChart(xvalueList, yvalueList, chart_barchart_jitai);
                }else{
                    initChart(new ArrayList<String>(), new ArrayList<Integer>(), chart_barchart_jitai);
                }
                RadioGroup operateRg=holder.getView(R.id.operate_rg);
                operateRg.setOnCheckedChangeListener(menuCheckedChangeListener);
                break;
                case TYPE_LEVEL_4:
                    TongjiRongChartBeen tongjiRongChartBeen=(TongjiRongChartBeen) itemTemp;
                  TextView  tvHegelv=holder.getView(R.id.tv_hegelv);
                  RingCustomView rcvHegelv=holder.getView(R.id.rcv_hegelv);
                    RingCustomView  rcv_bulianglv=holder.getView(R.id.rcv_bulianglv);
                    TextView tv_bulianglv=holder.getView(R.id.tv_bulianglv);
                    RingCustomView rcv_baofeilv=holder.getView(R.id.rcv_baofeilv);
                    TextView tv_baofeilv=holder.getView(R.id.tv_baofeilv);
                    setRCV(tvHegelv,rcvHegelv,tongjiRongChartBeen.getHege());
                    setRCV(tv_bulianglv,rcv_bulianglv,tongjiRongChartBeen.getBuliang());
                    setRCV(tv_baofeilv,rcv_baofeilv,tongjiRongChartBeen.getBaofei());
                    break;
        }

    }
    private class MenuCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
           // if(!group.isPressed())return;
            switch (checkedId) {

                case R.id.rb_yunxing:
                    type="yunxing";
                //  setWeekBar(dayType);
                  if(clickLinster!=null){
                      clickLinster.doServer();
                  }
                    break;
                case R.id.rb_lixian:
                    type="lixian";
                  //  setWeekBar(dayType);
                    if(clickLinster!=null){
                        clickLinster.doServer();
                    }
                    break;
                case R.id.rb_daiji:
                    type="daiji";
                  //  setWeekBar(dayType);
                    if(clickLinster!=null){
                        clickLinster.doServer();
                    }
                    break;
                case R.id.rb_guzhang:
                    type="guzhang";
                  //  setWeekBar(dayType);
                    if(clickLinster!=null){
                        clickLinster.doServer();
                    }
                    break;
            }
        }
    }
    public void setWeekBar(String week){
    //    week = week;
//        initChartSet(chart_barchart_jitai);
//        initChart(getXvalueList(week),getYvalueList(week,type),chart_barchart_jitai);
    }


    private  void setRCV(TextView tvbaofenbi, RingCustomView rcvView, float baifenbi){
        rcvView.setProgress((int) (baifenbi*1000));
        tvbaofenbi.setText(((int) (baifenbi * 1000))/10f + "%");
        //rc
    }
    private void initChart(List<String> xvalueList,List<Integer> yvalueList, BarChart chart_barchart_jitai){
        chart_barchart_jitai.setNoDataText(mContext.getString(R.string.no_chart_data));
        if(xvalueList.size()==0&&yvalueList.size()==0) {
            chart_barchart_jitai.setData(null);
        }else {
            int index = 0;
            List<BarEntry> yBarVals = new ArrayList<>();
            for (Integer yvalue : yvalueList) {
                yBarVals.add(new BarEntry(yvalue, index++));
            }
            BarDataSet set = new BarDataSet(yBarVals, "");
            set.setColor(mContext.getResources().getColor(R.color.main_green));//set会循环这里的颜色进行添加
            set.setValueTextColor(mContext.getResources().getColor(R.color.white));
            set.setValueTextSize(12f);
            set.setValueFormatter(new ValueFormatter() {

                @Override
                public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) {
                    return "" + (int) v;
                }
            });
            set.setHighlightEnabled(false);
            set.setDrawValues(true);
            BarData data = new BarData(xvalueList, set);
            chart_barchart_jitai.setData(data);
        }

    }
    private void initChartSet( BarChart chart_barchart_jitai) {

        YAxis yAxis = chart_barchart_jitai.getAxisLeft();
        yAxis.setTextColor(mContext.getResources().getColor(R.color.bwhite));
        yAxis.setDrawLabels(true);
        YAxis yAxist = chart_barchart_jitai.getAxisRight();
        yAxist.setDrawLabels(false);
        chart_barchart_jitai.getLegend().setEnabled(false);
        chart_barchart_jitai.animateY(1500);
        chart_barchart_jitai.setDescription("");
        chart_barchart_jitai.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        chart_barchart_jitai.getXAxis().setTextColor(mContext.getResources().getColor(R.color.bwhite));
        chart_barchart_jitai.getXAxis().setDrawAxisLine(false);
        chart_barchart_jitai.getXAxis().setDrawGridLines(false);
        chart_barchart_jitai.getAxisLeft().setDrawAxisLine(false);
        chart_barchart_jitai.getAxisLeft().setDrawGridLines(false);
        chart_barchart_jitai.getAxisRight().setDrawAxisLine(false);
        chart_barchart_jitai.getAxisRight().setDrawGridLines(true);
        chart_barchart_jitai.setDoubleTapToZoomEnabled(false);
        chart_barchart_jitai.setScaleEnabled(false);

    }

    private void setTSData(PieChart chart_jitai,float yunxing,float lixian,float jinggao,float daiji) {
        chart_jitai.setNoDataText(mContext.getString(R.string.no_chart_data));
        if(yunxing==0&&lixian==0&&jinggao==0&&daiji==0){
            chart_jitai.setData(null);
        }else {
            int allCount = (int) (yunxing + lixian + jinggao + daiji);
            chart_jitai.setCenterText(generateCenterText());
            //设置标题
            ArrayList<String> titles = new ArrayList<>();
            ArrayList<Entry> entrys = new ArrayList<>();
            int index = 0;
            List<Integer> colors = new ArrayList<>();
            if (yunxing != 0) {
                titles.add(mContext.getResources().getString(R.string.yunxing) + (int) ((yunxing / allCount) * 1000) / 10f + "%");
                if (yunxing < 0.01) {
                    yunxing = 0.01f;
                }
                entrys.add(new Entry(yunxing, index));
                colors.add(mContext.getResources().getColor(R.color.main_green));
                index++;
            }
            if (lixian != 0) {
                titles.add(mContext.getResources().getString(R.string.lixian) + (int) ((lixian / allCount) * 1000) / 10f + "%");
                if (lixian < 0.01) {
                    lixian = 0.01f;
                }
                entrys.add(new Entry(lixian, index));
                colors.add(mContext.getResources().getColor(R.color.main_gray));
                index++;
            }
            if (jinggao != 0) {
                titles.add(mContext.getResources().getString(R.string.jinggao) + (int) ((jinggao / allCount) * 1000) / 10f + "%");
                if (jinggao < 0.01) {
                    jinggao = 0.01f;
                }
                entrys.add(new Entry(jinggao, index));
                colors.add(mContext.getResources().getColor(R.color.main_red));
                index++;
            }
            if (daiji != 0) {
                titles.add(mContext.getResources().getString(R.string.daiji) + (int) ((daiji / allCount) * 1000) / 10f + "%");
                if (daiji < 0.01) {
                    daiji = 0.01f;
                }
                entrys.add(new Entry(daiji, index));
                colors.add(mContext.getResources().getColor(R.color.main_yellow));
                index++;
            }

            //饼图数据集
            PieDataSet dataset = new PieDataSet(entrys, "");
            //设置饼状图Item之间的间隙
            dataset.setSliceSpace(5f);
            dataset.setDrawValues(false);
            //饼图Item被选中时变化的距离
            dataset.setSelectionShift(5f);
            //颜色填充
            dataset.setColors(colors);
            //数据填充
            PieData piedata = new PieData(titles, dataset);
            //设置饼图上显示数据的字体大小
            piedata.setValueTextSize(12);
            piedata.setValueTextColor(mContext.getResources().getColor(R.color.bwhite));
            chart_jitai.setData(piedata);
        }
    }
    /**
     * 中间文字绘制
     *
     * @param
     * @return
     */
    private SpannableString generateCenterText() {
       // String total = Integer.toString(sum);
        SpannableString s = new SpannableString("设备运行情况");
      //  s.setSpan(new RelativeSizeSpan(3f), 0, total.length(), 0);
      //  s.setSpan(new ForegroundColorSpan(Color.rgb(88, 146, 240)), 0, total.length(), 0);
        s.setSpan(new ForegroundColorSpan(Color.rgb(153, 153, 153)), 0, s.length(), 0);
        return s;
    }
    private void initChartOne(PieChart chart) {
        //设置是否可转动
        chart.setRotationEnabled(false);
        //设置X轴的动画
        chart.animateX(1000);
        //使用百分比
        chart.setUsePercentValues(true);
        //设置图列可见
        chart.getLegend().setEnabled(false);
        chart.setDrawHoleEnabled(true);
        chart.setHoleColor(mContext.getResources().getColor(R.color.transt));
        chart.setDrawSliceText(true);
        chart.setDescription("");
        chart.setBackgroundColor(mContext.getResources().getColor(R.color.blueColorAppKey));
        chart.setCenterTextSize(12f);//设置中间文字中大小

    }
    public void setText(TextView tvCount,String context,String img) {
        String counStr="";
        if(!TextUtils.isEmpty(img)){
            String[] arrimgs=img.split("\\^");
            counStr=context + "照片("+arrimgs.length+")";
            SpannableStringBuilder style = new SpannableStringBuilder(counStr);
            style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blueColorAppKey)), context.length(), counStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //颜色
            tvCount.setText(style);
        }else{
            tvCount.setText(context);
        }

    }
    public interface ClickLinster{
        void doServer();
    }
}
